home *** CD-ROM | disk | FTP | other *** search
/ PC go! 2008 October / PCgo 2008-10 (DVD).iso / interface / contents / vollversionen_6617 / 21733 / files / xulrunner / modules / PluralForm.jsm < prev    next >
Encoding:
Text File  |  2008-08-20  |  7.4 KB  |  206 lines

  1. /* ***** BEGIN LICENSE BLOCK *****
  2.  *   Version: MPL 1.1/GPL 2.0/LGPL 2.1
  3.  *
  4.  * The contents of this file are subject to the Mozilla Public License Version
  5.  * 1.1 (the "License"); you may not use this file except in compliance with
  6.  * the License. You may obtain a copy of the License at
  7.  * http://www.mozilla.org/MPL/
  8.  *
  9.  * Software distributed under the License is distributed on an "AS IS" basis,
  10.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  11.  * for the specific language governing rights and limitations under the
  12.  * License.
  13.  *
  14.  * The Original Code is Plural Form l10n Code.
  15.  *
  16.  * The Initial Developer of the Original Code is
  17.  * Edward Lee <edward.lee@engineering.uiuc.edu>.
  18.  * Portions created by the Initial Developer are Copyright (C) 2008
  19.  * the Initial Developer. All Rights Reserved.
  20.  *
  21.  * Contributor(s):
  22.  *
  23.  * Alternatively, the contents of this file may be used under the terms of
  24.  * either the GNU General Public License Version 2 or later (the "GPL"), or
  25.  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  26.  * in which case the provisions of the GPL or the LGPL are applicable instead
  27.  * of those above. If you wish to allow use of your version of this file only
  28.  * under the terms of either the GPL or the LGPL, and not to allow others to
  29.  * use your version of this file under the terms of the MPL, indicate your
  30.  * decision by deleting the provisions above and replace them with the notice
  31.  * and other provisions required by the GPL or the LGPL. If you do not delete
  32.  * the provisions above, a recipient may use your version of this file under
  33.  * the terms of any one of the MPL, the GPL or the LGPL.
  34.  *
  35.  * ***** END LICENSE BLOCK ***** */
  36.  
  37. var EXPORTED_SYMBOLS = [ "PluralForm" ];
  38.  
  39. /**
  40.  * This module provides the PluralForm object which contains a method to figure
  41.  * out which plural form of a word to use for a given number based on the
  42.  * current localization. There is also a makeGetter method that creates a get
  43.  * function for the desired plural rule. This is useful for extensions that
  44.  * specify their own plural rule instead of relying on the browser default.
  45.  * (I.e., the extension hasn't been localized to the browser's locale.)
  46.  *
  47.  * See: http://developer.mozilla.org/en/docs/Localization_and_Plurals
  48.  *
  49.  * List of methods:
  50.  *
  51.  * string pluralForm
  52.  * get(int aNum, string aWords)
  53.  *
  54.  * int numForms
  55.  * numForms()
  56.  *
  57.  * [string pluralForm get(int aNum, string aWords), int numForms numForms()]
  58.  * makeGetter(int aRuleNum)
  59.  * Note: Basically, makeGetter returns 2 functions that do "get" and "numForm"
  60.  */
  61.  
  62. const Cc = Components.classes;
  63. const Ci = Components.interfaces;
  64.  
  65. const kIntlProperties = "chrome://global/locale/intl.properties";
  66.  
  67. // These are the available plural functions that give the appropriate index
  68. // based on the plural rule number specified. The first element is the number
  69. // of plural forms and the second is the function to figure out the index.
  70. let gFunctions = [
  71.   // 0: Chinese
  72.   [1, function(n) 0],
  73.   // 1: English
  74.   [2, function(n) n!=1?1:0],
  75.   // 2: French
  76.   [2, function(n) n>1?1:0],
  77.   // 3: Latvian
  78.   [3, function(n) n%10==1&&n%100!=11?1:n!=0?2:0],
  79.   // 4: Scottish Gaelic
  80.   [3, function(n) n==1?0:n==2?1:2],
  81.   // 5: Romanian
  82.   [3, function(n) n==1?0:n==0||n%100>0&&n%100<20?1:2],
  83.   // 6: Lithuanian
  84.   [3, function(n) n%10==1&&n%100!=11?0:n%10>=2&&(n%100<10||n%100>=20)?2:1],
  85.   // 7: Russian
  86.   [3, function(n) n%10==1&&n%100!=11?0:n%10>=2&&n%10<=4&&(n%100<10||n%100>=20)?1:2],
  87.   // 8: Slovak
  88.   [3, function(n) n==1?0:n>=2&&n<=4?1:2],
  89.   // 9: Polish
  90.   [3, function(n) n==1?0:n%10>=2&&n%10<=4&&(n%100<10||n%100>=20)?1:2],
  91.   // 10: Slovenian
  92.   [4, function(n) n%100==1?0:n%100==2?1:n%100==3||n%100==4?2:3],
  93.   // 11: Irish Gaeilge
  94.   [5, function(n) n==1?0:n==2?1:n>=3&&n<=6?2:n>=7&&n<=10?3:4],
  95.   // 12: Arabic
  96.   [4, function(n) n==1?0:n==2?1:n<=10?2:3],
  97.   // 13: Maltese
  98.   [4, function(n) n==1?0:n==0||n%100>0&&n%100<=10?1:n%100>10&&n%100<20?2:3],
  99.   // 14: Macedonian
  100.   [3, function(n) n%10==1?0:n%10==2?1:2],
  101. ];
  102.  
  103. let PluralForm = {
  104.   /**
  105.    * Get the correct plural form of a word based on the number
  106.    *
  107.    * @param aNum
  108.    *        The number to decide which plural form to use
  109.    * @param aWords
  110.    *        A semi-colon (;) separated string of words to pick the plural form
  111.    * @return The appropriate plural form of the word
  112.    */
  113.   get get()
  114.   {
  115.     // This method will lazily load to avoid perf when it is first needed and
  116.     // creates getPluralForm function. The function it creates is based on the
  117.     // value of pluralRule specified in the intl stringbundle.
  118.     // See: http://developer.mozilla.org/en/docs/Localization_and_Plurals
  119.  
  120.     // Delete the getters to be overwritten
  121.     delete PluralForm.numForms;
  122.     delete PluralForm.get;
  123.  
  124.     // Get the plural rule number from the intl stringbundle
  125.     let ruleNum = Number(Cc["@mozilla.org/intl/stringbundle;1"].
  126.       getService(Ci.nsIStringBundleService).createBundle(kIntlProperties).
  127.       GetStringFromName("pluralRule"));
  128.  
  129.     // Make the plural form get function and set it as the default get
  130.     [PluralForm.get, PluralForm.numForms] = PluralForm.makeGetter(ruleNum);
  131.     return PluralForm.get;
  132.   },
  133.  
  134.   /**
  135.    * Create a pair of plural form functions for the given plural rule number.
  136.    *
  137.    * @param aRuleNum
  138.    *        The plural rule number to create functions
  139.    * @return A pair: [function that gets the right plural form,
  140.    *                  function that returns the number of plural forms]
  141.    */
  142.   makeGetter: function(aRuleNum)
  143.   {
  144.     // Default to "all plural" if the value is out of bounds or invalid
  145.     if (aRuleNum < 0 || aRuleNum >= gFunctions.length || isNaN(aRuleNum)) {
  146.       log(["Invalid rule number: ", aRuleNum, " -- defaulting to 0"]);
  147.       aRuleNum = 0;
  148.     }
  149.  
  150.     // Get the desired pluralRule function
  151.     let [numForms, pluralFunc] = gFunctions[aRuleNum];
  152.  
  153.     // Return functions that give 1) the number of forms and 2) gets the right
  154.     // plural form
  155.     return [function(aNum, aWords) {
  156.       // Figure out which index to use for the semi-colon separated words
  157.       let index = pluralFunc(aNum ? Number(aNum) : 0);
  158.       let words = aWords ? aWords.split(/;/) : [""];
  159.  
  160.       // Explicitly check bounds to avoid strict warnings
  161.       let ret = index < words.length ? words[index] : undefined;
  162.  
  163.       // Check for array out of bounds or empty strings
  164.       if ((ret == undefined) || (ret == "")) {
  165.         // Report the caller to help figure out who is causing badness
  166.         let caller = PluralForm.get.caller ? PluralForm.get.caller.name : "top";
  167.  
  168.         // Display a message in the error console
  169.         log(["Index #", index, " of '", aWords, "' for value ", aNum,
  170.             " is invalid -- plural rule #", aRuleNum, "; called by ", caller]);
  171.  
  172.         // Default to the first entry (which might be empty, but not undefined)
  173.         ret = words[0];
  174.       }
  175.  
  176.       return ret;
  177.     }, function() numForms];
  178.   },
  179.  
  180.   /**
  181.    * Get the number of forms for the current plural rule
  182.    *
  183.    * @return The number of forms
  184.    */
  185.   get numForms()
  186.   {
  187.     // We lazily load numForms, so trigger the init logic with get()
  188.     PluralForm.get();
  189.     return PluralForm.numForms;
  190.   },
  191. };
  192.  
  193. /**
  194.  * Private helper function to log errors to the error console and command line
  195.  *
  196.  * @param aMsg
  197.  *        Error message to log or an array of strings to concat
  198.  */
  199. function log(aMsg)
  200. {
  201.   let msg = "PluralForm.jsm: " + (aMsg.join ? aMsg.join("") : aMsg);
  202.   Cc["@mozilla.org/consoleservice;1"].getService(Ci.nsIConsoleService).
  203.     logStringMessage(msg);
  204.   dump(msg + "\n");
  205. }
  206.